﻿
using QG.JobBack.Core;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace QG.JobBack.BLL.Service
{
    public class ModuleService : BaseRepository<Module>, IModuleService
    {
        /// <summary>
        /// 添加一个组
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public ResponseResult AddModule(Module module)
        {
            ResponseResult result = new ResponseResult();
            result.success = Add(module);
            result.msg = result.success ? "添加成功" : "添加失败";

            return result;
        }
        /// <summary>
        /// 修改一个组
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public ResponseResult UpdateModule(Module module)
        {
            ResponseResult result = new ResponseResult();
            //先查出对象
            var model = Query(a => a.Id == module.Id);
            //赋值
            model.ParentId = module.ParentId;
            model.Name = module.Name;
            model.SortId = module.SortId;
            model.Remark = module.Remark;
            model.IsEnable = module.IsEnable;

            result.success = Update(model);
            result.msg = result.success ? "修改成功" : "修改失败";

            return result;
        }

        /// <summary>
        /// 查询组列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult GetModuleList(int page,int limit, string moduleName)
        {
            //查询条件
            int total;
            ResponseResult result = new ResponseResult();
            Expression<Func<Module, bool>> whereLambda = a => true;
            if (!string.IsNullOrEmpty(moduleName))
            {
                whereLambda=a=>a.Name.Contains(moduleName);
            }
            Expression<Func<Module, int>> orderbyLambda = a => a.Id;
            var list = QueryPageList(page, limit, whereLambda, orderbyLambda, out total, true);
            //对象赋值
            result.code = 0;
            result.count = list.Count;
            result.msg = string.Empty;
            result.data = list;
            //返回结果
            return result;
        }
        /// <summary>
        /// 根据ID删除一个组
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public ResponseResult DeleteModuleById(int id)
        {
            ResponseResult result = new ResponseResult();
            result.success = DeleteById(id);
            result.msg = result.success ? "删除成功" : "删除失败";

            return result;
        }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult GetModule(int id)
        {
            ResponseResult result = new ResponseResult();
            result.data = Query(a => a.Id == id);
            return result;
        }

        /// <summary>
        /// 查询所有父级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult GetParentModule()
        {
            ResponseResult result = new ResponseResult();
            result.data = QueryList(a => a.ParentId == 0);
            return result;
        }


        /// <summary>
        /// 查询所有组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult GetModules()
        {
            ResponseResult result = new ResponseResult();
            result.data = QueryList(a => true);
            return result;
        }

        /// <summary>
        /// 根据ID修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ResponseResult UpdateStatus(int id,bool status)
        {
            ResponseResult result = new ResponseResult();

            //根据Id获取要修改的对象
            var model = Query(a => a.Id == id);
            if (model != null)
            {
                model.IsEnable = status;

                result.success=Update(model);
                return result;
            }

            return result;
        }


    }
}
